#!/bin/bash

if [ -z "$ANDROID_NDK" ]; then
  echo "在开始编译前，你必须定义 ANDROID_NDK 用于指出你的 NDK 目录."
  echo "例如：export ANDROID_NDK=/Users/keep/Dev/Android/SDK/ndk-bundle"
  exit 1
fi

WD=`pwd` #ndk 目录根据你的安装目录
if [ -z "$PLATFORM_VERSION" ]; then
  echo "没有定义默认的 PLATFORM_VERSION，将采用 24"
  PLATFORM_VERSION=24
fi

while [ -n "$1" ]
do
  case "$1" in
    --skip-toolchain)
      SKIP_TOOLCHAIN=1
      ;;
    --skip-x264)
      SKIP_X264=1
      ;;
    --skip-ffmpeg)
      SKIP_FFMPEG=1
      ;;
    --skip-pack)
      SKIP_PACK=1
      ;;
    *)
      ;;
  esac
  shift
done


# Detect OS
OS=`uname`
HOST_ARCH=`uname -m`
if [ $OS == 'Linux' ]; then
  export HOST_SYSTEM=linux-$HOST_ARCH
elif [ $OS == 'Darwin' ]; then
  export HOST_SYSTEM=darwin-$HOST_ARCH
fi

CFLAGS="-std=c99 -O3 -Wall -pipe -fpic -fasm \
  -finline-limit=300 -ffast-math \
  -fstrict-aliasing -Werror=strict-aliasing \
  -Wno-psabi -Wa,--noexecstack \
  -fdiagnostics-color=always \
  -DANDROID -DNDEBUG"

LDFLAGS="-lm -lz -Wl,--no-undefined -Wl,-z,noexecstack"

TOOLCHAIN=/tmp/KeepVideo #toolchain 安装目录
SYSROOT=${TOOLCHAIN}/sysroot/
PLATFORM=${TOOLCHAIN}/platforms/android-${PLATFORM_VERSION}/arch-arm
BUILD_PREFIX=${WD}/libs #编译结果的目录
PREFIX=${WD}/FFmpegUtilsLib/libs/armeabi-v7a #编译结果的目录

echo ""
echo "NDK=${ANDROID_NDK}"
echo "PLATFORM_VERSION=${PLATFORM_VERSION}"
echo "TOOLCHAIN=${TOOLCHAIN}"
echo "SYSROOT=${SYSROOT}"
echo "BUILD_PREFIX=${BUILD_PREFIX}"
echo "PREFIX=${PREFIX}"
echo ""
echo "----- 以上是编译时变量 -----"
echo

#生成toolchain目录，这一段可以在第一次运行后注释掉
 echo ============ Phase.1 生成编译工具链 ============
 echo

 ${ANDROID_NDK}/build/tools/make_standalone_toolchain.py \
     --arch arm \
     --api ${PLATFORM_VERSION} --install-dir ${TOOLCHAIN} --force

 echo
 echo ============ Phase.2 编译 x264 ============
 echo

 cd x264

 make uninstall
 make distclean

 CC=gcc
 CXX=g++

 ./configure \
     --prefix=${BUILD_PREFIX} \
     --enable-static \
     --enable-shared \
     --enable-pic \
     --disable-asm \
     --disable-cli \
     --disable-opencl \
     --host=arm-linux \
     --cross-prefix=${TOOLCHAIN}/bin/arm-linux-androideabi- \
     --sysroot=${SYSROOT} \
     --extra-cflags="-march=armv7-a -marm"
#      --host=arm-linux-androideabi \
#    --extra-cflags="-D__ANDROID_API__=21 -isystem ${PLATFORM}" \
 #    --extra-ldflags="--sysroot=${PLATFORM}"

 make -j8 || exit 1
 make install || exit 1

 cd ..

 echo
 echo ============ Phase.3 编译 FFmpeg ============
 echo

 # 加入x264编译库
 FFMPEG_CFLAGS="-I${BUILD_PREFIX}/include -march=armv7-a \
 -mfpu=vfpv3-d16 \
 -mfloat-abi=softfp \
 -marm"
 FFMPEG_LDFLAGS="-L${BUILD_PREFIX}/lib -ffast-math \
 -fstrict-aliasing -Werror=strict-aliasing \
 -Wno-psabi -Wa,--noexecstack"
 #FFMPEG_CFLAGS="-I${BUILD_PREFIX}/include"
 #FFMPEG_LDFLAGS="-L${BUILD_PREFIX}/lib"

 cd ffmpeg

 echo "配置中..."
 echo

 make uninstall
 make distclean

 ./configure \
     --target-os=android \
     --prefix=${BUILD_PREFIX} \
     --enable-cross-compile \
     --disable-doc \
     --arch=arm \
     --cc=${TOOLCHAIN}/bin/arm-linux-androideabi-gcc \
     --cross-prefix=${TOOLCHAIN}/bin/arm-linux-androideabi- \
     --disable-stripping \
     --nm=${TOOLCHAIN}/bin/arm-linux-androideabi-nm \
     --sysroot=${SYSROOT} \
     --enable-jni \
     --enable-mediacodec \
     --enable-decoder=h264_mediacodec \
     --enable-hwaccel=h264_mediacodec \
     --enable-neon \
     --enable-gpl \
     --enable-shared \
     --enable-static \
     --enable-version3 \
     --enable-small \
     --disable-vda \
     --disable-iconv \
     --disable-encoders \
     --enable-libx264 \
     --enable-encoder=libx264 \
     --enable-encoder=mjpeg \
     --enable-encoder=png \
     --enable-encoder=aac \
     --disable-muxers \
     --enable-muxer=flv \
     --enable-muxer=mov \
     --enable-muxer=ipod \
     --enable-muxer=psp \
     --enable-muxer=mp4 \
     --enable-muxer=avi \
     --disable-decoders \
     --enable-decoder=aac \
     --enable-decoder=aac_latm \
     --enable-decoder=h264 \
     --enable-decoder=mpeg4 \
     --enable-decoder=mjpeg \
     --enable-decoder=png \
     --disable-demuxers \
     --enable-demuxer=image2 \
     --enable-demuxer=h264 \
     --enable-demuxer=aac \
     --enable-demuxer=avi \
     --enable-demuxer=mpc \
     --enable-demuxer=mov \
     --disable-parsers \
     --enable-parser=aac \
     --enable-parser=ac3 \
     --enable-parser=h264 \
     --enable-avresample \
     --disable-protocols \
     --enable-protocol=file \
     --enable-protocol=rtmp \
     --disable-bsfs \
     --enable-bsf=aac_adtstoasc \
     --enable-bsf=h264_mp4toannexb \
     --disable-indevs \
     --enable-zlib \
     --disable-outdevs \
     --disable-ffprobe \
     --disable-ffplay \
     --disable-ffmpeg \
     --disable-ffserver \
     --disable-debug \
     --extra-cflags="${FFMPEG_CFLAGS}" \
     --extra-ldflags="${FFMPEG_LDFLAGS}"

 [ $PIPESTATUS == 0 ] || exit 1

 echo "开始编译..."

 make -j8 || exit 1
 make install || exit 1

echo
echo ============ Phase.4 复制至libs  ============
echo
#
cp -R ${BUILD_PREFIX}/include ${PREFIX}/..
cp ${BUILD_PREFIX}/lib/*.a ${PREFIX}/
cp ${BUILD_PREFIX}/lib/*.so ${PREFIX}/

cd ${BUILD_PREFIX}/lib

#${TOOLCHAIN}/bin/arm-linux-androideabi-ld \
#    -rpath-link=${SYSROOT}/usr/lib \
#    -L${SYSROOT}/usr/lib \
#    -L${BUILD_PREFIX}/lib \
#    -soname libffmpeg.so \
#    -shared -nostdlib -Bsymbolic --whole-archive --no-undefined \
#    -o ${PREFIX}/libffmpeg.so \
#    ./libx264.a \
#    ./libavcodec.a \
#    ./libavfilter.a \
#    ./libavresample.a \
#    ./libswresample.a \
#    ./libavformat.a \
#    ./libavutil.a \
#    ./libswscale.a \
#    ./libpostproc.a \
#    ./libavdevice.a \
#    -lc -lm -lz -ldl -llog \
#    ${TOOLCHAIN}/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a \
#    || exit 1

cd ..
#
#cp -R ${BUILD_PREFIX}/include ${PREFIX}/..

echo
echo ============ 编译完成 ============
echo

